<?php
class IndexAction extends BaseAction {
	public function index() {
		if (empty ( $_SESSION ['userid'] )) {
			$this->assign ( 'jumpUrl', U ( "Auth/login" ) );
			$this->error ( "请先登陆." );
		}
		self::autogive();
		self::autocv();
		$this->display ();
	}
	
	// 每日执行分期脚本
	function script() {
		return;
		$sta_tmp = M ( 'user_give' )->where ( "`status`=1 and countoedu>0 
			and (qishu-((" . date ( 'Y' ) . "-FROM_UNIXTIME(startday,'%Y'))*12+" . date ( 'm' ) . "-FROM_UNIXTIME(startday,'%m')))>-2" )->select ();
		//dump(M ( 'user_give' )->getlastsql());
		//exit;
		foreach ( $sta_tmp as $value ) {
			$date_arr = array ();
			$date_arr [] = date ( 'Ymd', $value ['startday'] );
			for($i = 1; $i <= $value ['qishu']; $i ++) {
				$date_arr [] = date ( 'Ymd', strtotime ( "+{$i} month", $value ['startday'] ) );
			}
			if ( in_array( date ( 'Ymd' ), $date_arr ) && $value['notoveroedu'] ) {
				$is_give_log = M()->query( "select id from user_give_logs 
					where userid={$value ['userid']} and only='{$value ['only']}' and from_unixtime(createtime,'%Y%m%d')='".date('Ymd')."'" );
				if ( $is_give_log ) {continue;}
				$val = M ( 'user_money' )->field ( 'value' )->where ( array (
						'userid' => $value ['userid'],
						'subject' => $value ['moneytype'] 
				) )->find ();
				$data = array (
						'id' => $value ['id'],
						'overqishu' => $value ['overqishu'] + 1,
						'notoverqishu' => $value ['qishu'] - $value ['overqishu'] - 1,
						'overoedu' => $value ['overoedu'] + $value ['eachoedu'],
						'notoveroedu' => $value ['countoedu'] - $value ['overoedu'] - $value ['eachoedu'],
						'updatetime' => time (),
						'isover' => ($value ['qishu'] - $value ['overqishu'] - 1) ? 1 : 0 
				);
				M ( 'user_give' )->save ( $data );
				
				$data = array (
						'userid' => $value ['userid'],
						'only' => $value ['only'],
						'mon' => date ( 'm' ),
						'addoedu' => $value ['eachoedu'],
						'oldoedu' => $value ['notoveroedu'],
						'nowoedu' => $value ['notoveroedu'] - $value ['eachoedu'],
						'createtime' => time () 
				);
				M ( 'user_give_logs' )->add ( $data );
				
				$data = array (
						'userid' => $value ['userid'],
						'action_userid' => 0,
						'platform' => 'SMW01',
						'subject' => $value ['moneytype'],
						'value' => $value ['eachoedu'],
						'nowvalue' => $val ['value'] + $value ['eachoedu'],
						'ctime' => time (),
						'cip' => get_client_ip (),
						'bzvu' => '分期到账',
						'changtype' => 'ADMIN_ADD' 
				);
				M ( 'user_assets' )->add ( $data );
				
				$data = array (
						'userid' => $value ['userid'],
						'subject' => $value ['moneytype'],
						'platform' => 'SMW01',
						'value' => $val ['value'] + $value ['eachoedu'],
						'mtime' => time(),
				);
				M ( 'user_money' )->add ( $data,array(),true );
			}
		}
	}
	
	/**
	 * 自动赠送分期到账的BV
	 */
	protected function autogive() {
		// 查询更新时间距离现在超过30天的记录
		if(time()<gettime('2014-3-26')){
			$baktime = time () - 20 * 24 * 60 * 60;
			if($_GET['iic']=='demo'){
				dump(20);
			}
		}else{
			$baktime = time () - 30 * 24 * 60 * 60;
		}
		$give = M ( "user_give" );
		$givelogs = M ( "user_give_logs" );
		$user = M ( "User" );
		$where = array ();
		$where ['updatetime'] = array (
				'lt',
				$baktime
		);
		$where ['qishu'] = array (
				'gt',
				0
		);
		// $where['overqishu']=array('lt','`qishu`');
		$where ['notoverqishu'] = array (
				'gt',
				'0'
		);
		$where ['notoveroedu'] = array (
				'gt',
				'0'
		);
		$where ['status'] = array (
				'eq',
				'1'
		);
		$where ['isover'] = array (
				'eq',
				'0'
		);
		$where ['moneytype'] = array (
				'eq',
				'BV'
		);
		$list = array ();
		$list = $give->where ( $where )->limit ( '0,30' )->select ();
		if($_GET['iic']=='demo'){
			dump($give->getLastSql());
		}
		if ($list) {
			foreach ( $list as $key => $v ) {
				if (! ($v ['overqishu'] < $v ['qishu'])) {
					continue;
				}
				$time = time ();
				$gdata = array ();
				$maps = array ();
				$udata = array ();
				$maps ['only'] = $v ['only'];
				//$maps ['mon'] = date ( 'm' );
				$maps['_query'] = "FROM_UNIXTIME(createtime,'%Y%m')=".date ( 'Ym' );
				$maps ['userid'] = array (
						'eq',
						$v ['userid']
				);
				$aaaa = $givelogs->where ( $maps )->find ();
				if ($aaaa) {
					continue;
				} else {
					$maps ['addoedu'] = $v ['eachoedu'];
					//$uinfo = $user->field ( 'id,give' )->where ( "id={$v['userid']}" )->find ();
					$uinfo = M('user_money ')->field('userid,value')->where( array("userid"=>$v['userid'],'subject'=>'BV') )->find();
					$uinfo['value'] = $uinfo['value'] ? $uinfo['value'] : 0;
					$maps ['userid'] = $v ['userid'];
					$maps ['oldoedu'] = $uinfo ['value'];
					$maps ['nowoedu'] = $uinfo ['value'] + $v ['eachoedu'];
					$maps ['createtime'] = $time;
					$maps ['only'] = $v ['only'];
					
					$udata ['userid'] = $v['userid'];
					$udata ['mtime'] = $time;
					$udata ['value'] = $maps ['nowoedu'];
					$udata ['userid'] = $v['userid'];
					$udata ['subject'] = 'BV';
					$udata ['platform'] = 'SMW01';
						
					$gdata ['id'] = $v ['id'];
					$gdata ['userid'] = $v ['userid'];
					$gdata ['updatetime'] = $time;
					$gdata ['overqishu'] = $v ['overqishu'] + 1;
					$gdata ['notoverqishu'] = $v ['notoverqishu'] - 1;
					$gdata ['overoedu'] = $v ['overoedu'] + $v ['eachoedu'];
					$gdata ['notoveroedu'] = $v ['notoveroedu'] - $v ['eachoedu'];
					$gdata ['isover'] = $gdata ['overqishu'] == $v ['qishu'] ? 1 : 0;
					$maps ['mon'] = date ( 'm' );
					$givelogs->add ( $maps );
					$give->save ( $gdata );
					
					//修改余额
					//$user->where ( 'id=' . $v ['userid'] )->save ( $udata );
					M('user_money ')->add( $udata,array(),true );
					//写入财务流水
					//$this->_wuserassets ( 0, $v ['eachoedu'], $v ['userid'], $v ['auth'] );
					$data = array (
							'userid' => $v ['userid'],
							'action_userid' => 0,
							'platform' => 'SMW01',
							'subject' => 'BV',
							'value' => $v ['eachoedu'],
							'nowvalue' => $maps ['nowoedu'],
							'ctime' => time (),
							'cip' => get_client_ip (),
							'bzvu' => '分期到账：'.$v ['only'],
							'changtype' => 'ADMIN_ADD' 
					);
					M ( 'user_assets' )->add ( $data );
					
					$userinfo = M('user')->field('mobile')->where( array('id'=>$v['userid']) )->find();
					if ( $userinfo['mobile'] ) {
						$msg = date ( 'm月d日H时i分' ) . 'BV分期到账' . $v ['eachoedu']/100 . 'BV,';
						$msg .= '现在BV余额:' . $udata ['value']/100;
						$msg = iconv ( "UTF-8", "GB2312//TRANSLIT", $msg );
						sendMobiMsg ( $userinfo ['mobile'], $msg );
					}
				}
			}
		}
	}
	
	/**
	 * 自动赠送分期到账的CV
	 */
	protected function autocv() {
		// 查询更新时间距离现在超过30天的记录
		if(time ()<gettime('2014-3-26')){
			$baktime = time () - 20 * 24 * 60 * 60;
		}else{
			$baktime = time () - 30 * 24 * 60 * 60;
		}
		$give = M ( "user_give" );
		$givelogs = M ( "user_give_logs" );
		$user = M ( "User" );
		$where = array ();
		$where ['updatetime'] = array (
				'lt',
				$baktime
		);
		$where ['qishu'] = array (
				'gt',
				0
		);
		// $where['overqishu']=array('lt','`qishu`');
		$where ['notoverqishu'] = array (
				'gt',
				'0'
		);
		$where ['notoveroedu'] = array (
				'gt',
				'0'
		);
		$where ['status'] = array (
				'eq',
				'1'
		);
		$where ['isover'] = array (
				'eq',
				'0'
		);
		$where ['moneytype'] = array (
				'eq',
				'CV'
		);
		$list = array ();
		$list = $give->where ( $where )->limit ( '0,30' )->select ();
		if($_GET['iic']=='demo'){
			dump($give->getLastSql());
		}
		if ($list) {
			foreach ( $list as $key => $v ) {
				if (! ($v ['overqishu'] < $v ['qishu'])) {
					continue;
				}
				// dump($v);
				$time = time ();
				$gdata = array ();
				$maps = array ();
				$udata = array ();
				$maps ['only'] = $v ['only'];
				//$maps ['mon'] = date ( 'm' );
				$maps['_query'] = "FROM_UNIXTIME(createtime,'%Y%m')=".date ( 'Ym' );
				$maps ['userid'] = array (
						'eq',
						$v ['userid']
				);
				if ($givelogs->where ( $maps )->find ()) {
					continue;
				} else {
					$maps ['addoedu'] = $v ['eachoedu'];
					//$uinfo = $user->field ( 'id,amount,mobile,username' )->where ( "id={$v['userid']}" )->find ();
					$uinfo = M('user_money ')->field('value')->where( array("userid"=>$v['userid'],'subject'=>'CV') )->find();
					$uinfo['value'] = $uinfo['value'] ? $uinfo['value'] : 0;
					$maps ['userid'] = $v ['userid'];
					$maps ['oldoedu'] = $uinfo ['value'];
					$maps ['nowoedu'] = $uinfo ['value'] + $v ['eachoedu'];
					$maps ['createtime'] = $time;
					$maps ['only'] = $v ['only'];
						
					$udata ['userid'] = $v['userid'];
					$udata ['mtime'] = $time;
					$udata ['value'] = $maps ['nowoedu'];
					$udata ['userid'] = $v['userid'];
					$udata ['subject'] = 'CV';
					$udata ['platform'] = 'SMW01';
						
					$gdata ['id'] = $v ['id'];
					$gdata ['userid'] = $v ['userid'];
					$gdata ['updatetime'] = $time;
					$gdata ['overqishu'] = $v ['overqishu'] + 1;
					$gdata ['notoverqishu'] = $v ['notoverqishu'] - 1;
					$gdata ['overoedu'] = $v ['overoedu'] + $v ['eachoedu'];
					$gdata ['notoveroedu'] = $v ['notoveroedu'] - $v ['eachoedu'];
					$gdata ['isover'] = $gdata ['overqishu'] == $v ['qishu'] ? 1 : 0;
					
					$maps ['mon'] = date ( 'm' );
					$givelogs->add ( $maps );
					$give->save ( $gdata );
					
					//修改余额
					//$user->where ( 'id=' . $v ['userid'] )->save ( $udata );
					M('user_money ')->add( $udata,array(),true );
					//写入财务流水
					//$this->_wuserassets ( 0, $v ['eachoedu'], $v ['userid'], $v ['auth'] );
					$data = array (
							'userid' => $v ['userid'],
							'action_userid' => 0,
							'platform' => 'SMW01',
							'subject' => 'CV',
							'value' => $v ['eachoedu'],
							'nowvalue' => $maps ['nowoedu'],
							'ctime' => time (),
							'cip' => get_client_ip (),
							'bzvu' => '分期到账：'.$v ['only'],
							'changtype' => 'ADMIN_ADD' 
					);
					M ( 'user_assets' )->add ( $data );
					
					$userinfo = M('user')->field('mobile')->where( array('id'=>$v['userid']) )->find();
					if ( $userinfo['mobile'] ) {
						$msg = date ( 'm月d日H时i分' ) . 'CV分期到账' . $v ['eachoedu']/100 . 'CV,';
						$msg .= '现在CV余额:' . $udata ['value']/100;
						$msg = iconv ( "UTF-8", "GB2312//TRANSLIT", $msg );
						sendMobiMsg ( $userinfo ['mobile'], $msg );
					}
				}
			}
		}
	}
	
	/**
	 *我的工作台
	 *@date 2013-12-13
	 *@time 上午10:48:58
	 */
	function home() {
	//我的工作台
		
	}//end home
	
	/**
	 * 新闻公告
	 * @date 2013-12-13
	 * @time 上午10:46:06
	 */
	function news() {
		// 新闻公告
		$this->display ();
	} // end news
	
	
	/**
	 * 恢复价格为前一日的价格
	 * @date 2014-4-28
	 * @time 下午12:54:06
	 */
	function reprice() {
		// 恢复价格为前一日的价格
		exit;
		$sql="SELECT pb.*,p.price FROM product AS p LEFT JOIN product_price_b2b AS pb ON p.id=pb.product_id WHERE pb.price='12345600' AND p.status=0;";
		$list=M()->query($sql);
		$pricedao=M('ProductPriceB2b');
		$product=M('Product');
		$dao=M("ProductPriceB2bLogs");
		$rearr=array();
		foreach ($list as $v){
			$res=array();
			$where=array();
			$where['pid']=$v['product_id'];
			$where['jiayi']=array('NEQ','999999.99');
			$res=$dao->where($where)->order('id DESC')->find();
			if($res){
				$res['all']=unserialize($res['allprice']);
				$pricedata=array();
				$pricedata['price']=$res['all']['price'];
				$pricedata['jiayi']=$res['all']['jiayi'];
				$pricedata['jiaer']=$res['all']['jiaer'];
				$pricedata['jiasan']=$res['all']['jiasan'];
				$pricedata['jiasi']=$res['all']['jiasi'];
				$pricedata['jiawu']=$res['all']['jiawu'];
				$pricedata['jialiu']=$res['all']['jialiu'];
				$price_where=array();
				$price_where['product_id']=$v['product_id'];
				$pricedao->where($price_where)->save($pricedata);
				$rearr[]=$pricedao->getLastSql();
				$product_where=array();
				$product_data=array();
				$product_where['id']=$v['product_id'];
				$product_data['price']=$res['all']['jiayi'];
				$product->where($product_where)->save($product_data);
			}
		}
		dump($rearr);
	} // end reprice
}
